#!/bin/bash

echo "=== YOLOv8材料检测项目测试 ==="
echo ""

# 检查项目结构
echo "1. 检查项目结构..."
if [ -f "CMakeLists.txt" ]; then
    echo "✓ CMakeLists.txt 存在"
else
    echo "✗ CMakeLists.txt 不存在"
    exit 1
fi

if [ -d "include" ]; then
    echo "✓ include 目录存在"
else
    echo "✗ include 目录不存在"
    exit 1
fi

if [ -d "src" ]; then
    echo "✓ src 目录存在"
else
    echo "✗ src 目录不存在"
    exit 1
fi

# 检查关键文件
echo ""
echo "2. 检查关键文件..."
key_files=(
    "include/det/postprocess.h"
    "include/det/preprocess.h"
    "include/common.hpp"
    "src/postprocess.cpp"
    "src/camera_detect.cpp"
    "src/test_yolov8.cpp"
)

for file in "${key_files[@]}"; do
    if [ -f "$file" ]; then
        echo "✓ $file 存在"
    else
        echo "✗ $file 不存在"
    fi
done

# 检查依赖
echo ""
echo "3. 检查依赖库..."
if pkg-config --exists opencv4; then
    echo "✓ OpenCV4 已安装"
    pkg-config --modversion opencv4
else
    echo "✗ OpenCV4 未安装"
fi

# 尝试编译
echo ""
echo "4. 尝试编译项目..."
if [ -d "build" ]; then
    rm -rf build
fi

mkdir build
cd build

if cmake .. > /dev/null 2>&1; then
    echo "✓ CMake 配置成功"
else
    echo "✗ CMake 配置失败"
    cd ..
    exit 1
fi

if make -j$(nproc) > /dev/null 2>&1; then
    echo "✓ 编译成功"
else
    echo "✗ 编译失败"
    cd ..
    exit 1
fi

cd ..

# 检查可执行文件
echo ""
echo "5. 检查可执行文件..."
if [ -f "build/test_yolov8" ]; then
    echo "✓ test_yolov8 构建成功"
else
    echo "✗ test_yolov8 构建失败"
fi

if [ -f "build/camera_detect" ]; then
    echo "✓ camera_detect 构建成功"
else
    echo "✗ camera_detect 构建失败"
fi

# 运行测试程序
echo ""
echo "6. 运行测试程序..."
if [ -f "build/test_yolov8" ]; then
    echo "运行 test_yolov8..."
    timeout 10s ./build/test_yolov8 > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        echo "✓ test_yolov8 运行成功"
    else
        echo "✗ test_yolov8 运行失败或超时"
    fi
fi

echo ""
echo "=== 测试完成 ==="
echo ""
echo "如果所有测试都通过，项目应该可以正常工作。"
echo ""
echo "使用方法:"
echo "  ./build/camera_detect [模型文件] [摄像头ID]"
echo "  ./build/test_yolov8"
echo ""
echo "注意: 需要准备YOLOv8的RKNN模型文件才能进行实际检测。" 